package com.example.weektwo.dailysupplies.two;

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.weektwo.AmountView;
import com.example.weektwo.Bean.ContentDatas;
import com.example.weektwo.Bean.InfoBean;
import com.example.weektwo.R;
import com.example.weektwo.dailysupplies.Three.ShoppingCartActivity;
import com.squareup.picasso.Picasso;

import java.io.Serializable;
import java.util.List;

public class ChoiceAdapter extends BaseAdapter {
    private static final String TAG = "ChoiceAdapter";
    Context context;
    List<InfoBean> beans;


    public ChoiceAdapter(Context context, List<InfoBean> beans) {
        this.context = context;
        this.beans = beans;
    }

    @Override
    public int getCount() {
        return beans.size();
    }

    @Override
    public Object getItem(int position) {
        return beans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView==null) {
            convertView= LayoutInflater.from(context).inflate(R.layout.item_choice_lv,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }

        final InfoBean infoBean = beans.get(position);
        String title = infoBean.getTitle();//商品名称
        String kind = infoBean.getKind();//商品类别
        String pic = infoBean.getPic();//图片
        double price = infoBean.getPrice();//价格
        int count = infoBean.getCount();
        holder.name.setText(title);
        holder.kind.setText(kind);
        if (!TextUtils.isEmpty(pic)) {//判断图片是否为空
            Picasso.with(context).load(pic).into(holder.shopIv);
        }
        holder.price.setText(String.valueOf(price));
        holder.count.setGoods_storage(infoBean.getCount());//设置最大值

        holder.count.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount) {

            }
        });
        //     为购买的商品设置点击按钮事件
        final ViewHolder finalHolder = holder;
        holder.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String amountText = finalHolder.count.getAmountText();
                int count = Integer.parseInt(amountText);  //购买数量
                infoBean.setBuycount(count);
//                添加商品
                ContentDatas.buyList.add(infoBean);

            }
        });

        return convertView;
    }
    class ViewHolder{
       ImageView shopIv;
       TextView name,kind,price;
       AmountView count;
       Button submit;
       public ViewHolder(View view){
           shopIv=view.findViewById(R.id.item_choice_iv);
           name=view.findViewById(R.id.item_shop_name);
           kind=view.findViewById(R.id.choice_shop_kind);
           price=view.findViewById(R.id.item_choice_price);
           count=view.findViewById(R.id.choice_count);
           submit=view.findViewById(R.id.choice_submit);
       }
    }
}
